﻿/*{
author=pk2.2（기마책사、江东新风、白马叔叔、铃）
date=2023/05/17
}*/

// ## 2023/11/17 # 铃 # 完全汉化,优化逻辑 ##
// ## 2023/05/17 # 铃 # 修复大城市人才府会对不同势力小城市加成效果 ##
// ## 2023/05/08 # 铃 # 连续探索功能添加到小城市--注意非己方城市的人才府也会影响小城市行动力 ##
// ## 2021/01/28 # 白马叔叔 # 政治从高到低排序 ##
// ## 2021/01/02 # 江东新风 # 在连续探索时临时将舌战设为不观看，所以平时不需要将舌战设为不观看也可以连续探索 ##
// ## 2020/07/26 ##
/*
@ 만든이: 기마책사
@ UPDATE: '19.5.3    / 최초적용 : 인재探索 连续실행 메뉴
@ UPDATE: '19.5.8    / 변경내용 : 설전 옵션 체크 기능 추가 (설전OFF 시에만 连续探索 사용가능)
*/

namespace 连续인재探索 
{
	//---------------------------------------------------------------------------------------
    
	
	class Main
	{
		pk::building@ building;
		pk::force@ force;
		pk::city@ city;
        pk::person@ taishu;
        pk::district@ district;
        pk::list<pk::person@> person_list;
        int ACTION_COST_探索;

		Main()
		{
			pk::menu_item 连续探索;
			连续探索.menu = 102;   
			连续探索.pos = 9;
			连续探索.shortcut = global_menu::shortcut_连续探索;
            连续探索.init = pk::building_menu_item_init_t(init);
			连续探索.is_visible = pk::menu_item_is_visible_t(isVisible_连续探索);
			连续探索.is_enabled = pk::menu_item_is_enabled_t(isEnabled_连续探索);
			连续探索.get_text = pk::menu_item_get_text_t(getText_连续探索);
			连续探索.get_desc = pk::menu_item_get_desc_t(getDesc_连续探索);
			连续探索.handler = pk::menu_item_handler_t(handler_连续探索);
			pk::add_menu_item(连续探索);
            
		}
        
		void init(pk::building@ building_t)
		{
			@building = @building_t;
			@force = pk::get_force(building.get_force_id());
            int city_id = pk::get_city_id(building.pos);
            @city = pk::get_city(city_id);
            @taishu = pk::get_person(pk::get_taishu_id(building));
            @district = pk::get_district(building.get_district_id());
            
            person_list = pk::get_idle_person_list(building);   
            person_list.sort(function(a, b)
            {
				return a.stat[3] > b.stat[3];  // 按政治排序
            });

            ACTION_COST_探索 = get_hrcmd_ap(city,building);
            
		}
        

        int get_hrcmd_ap(pk::city@ city_t ,pk::building@ building_t)
        {
            int hrcmd_ap = 20;
            if (pk::is_alive(city_t))
            {
                if (pk::has_facility(city, 设施_人材府)  and city_t.get_force_id()==building_t.get_force_id())
                    return hrcmd_ap / 2;
            }
            return hrcmd_ap;
        }
        
        
        //---------------------------------------------------------------------------------------

		string getText_连续探索()
		{
			return pk::encode("连续探索");  
		}

		string getDesc_连续探索()
		{
       
            if (person_list.count == 0)
                return pk::encode("没有可执行的武将.");
            else if (int(district.ap) < ACTION_COST_探索)
                return pk::encode(pk::format("行动力不足 (必须 {} 行动力)", ACTION_COST_探索));
			else
				return pk::encode(pk::format("探索城市 (行动力{})", ACTION_COST_探索));
		}

		bool isEnabled_连续探索()
		{ 
            //if (debate_opt < 2) return false;
            
            if (person_list.count == 0) return false;
            else if (int(district.ap) < ACTION_COST_探索) return false;
			return true;
		}

        bool isVisible_连续探索()
        {
            if (pk::is_campaign()) return false;   
            if (!pk::is_alive(building)) return false;
            return true;
        }
        
		bool handler_连续探索()
		{
			if (person_list.count == 0) return false;
            
            int max = district.ap / ACTION_COST_探索;
            if (max == 0) return false;
            
            // 실행무장 선택하기
            pk::list<pk::person@> actors;
            string person_name;
            bool confirm = false;
            while (!confirm)
            {
            	// actors = UI::person_selector(pk::encode("选择搜索武将"), pk::encode("请选择要搜索的武将"), person_list, 1, pk::min(max, person_list.count), actors, { 武将能力_政治 }, 特技_眼力, true);
                actors = pk::person_selector(pk::encode("选择搜索武将"), pk::encode("请选择要搜索的武将"), person_list, 1, pk::min(max, person_list.count), actors, 26);
                int count = int(actors.count);
                if (count == 0) return false; 
                
                person_name = pk::decode( pk::get_name(pk::get_person(actors[0].get_id())) );
                if (count > 1)
                    person_name = pk::format("{}及其他{}人", person_name, count-1);
            
                confirm = pk::yes_no(pk::encode(pk::format("让\x1b[1x{}\x1b[0x探索吗?", person_name)));
            }
			//暂存舌战配置信息，将舌战设置为不观看，舌战后恢复
			int debate_opt = int(pk::option["Debate"]);
            if (int(pk::option["Debate"]) < 2)
			{
				pk::option["Debate"] = 2;
			}				
            
            
            // 连续 探索 실행
            for (int i = 0; i < actors.count; i++)
            {    
                pk::person@ actor = pk::get_person(actors[i].get_id());

                // 探索 명령
                pk::search_cmd_info cmd_search;
                @cmd_search.base  = building;
                @cmd_search.actor = actor;
                pk::command(cmd_search);
            }
			
			//如果在前一个配置时强制退出，则此处大概无法恢复
			pk::option["Debate"] = debate_opt;
            
            
			return true;
		}
        
        //---------------------------------------------------------------------------------------
        
        bool 调试模式 = false;

	}

	Main main;
}